{include file='common/head'}
<div style="overflow-x: hidden;width: 96%;margin: auto;padding-top: 1rem;">
    <div class="layui-form layui-form-pane">
        <input type="hidden" name="id" value="{$data.id|default=''}">
        <div class="layui-form-item">
            <div class="layui-form-item layui-col-xs6">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" value="{$data.title | default=''}" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">文章类型</label>
            <div class="layui-input-inline">
                <select name="type" lay-verify="required">
                    <option value="1" {if isset($data)}{if $data.type == 1}selected{/if}{/if}>行业干货</option>
                    <option value="2" {if isset($data)}{if $data.type == 2}selected{/if}{/if}>最新资讯</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <textarea id="content" style="display: none;">{$data.content | default=''}</textarea>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
                <button type="button" class="layui-btn layui-btn-primary" id="closeIframe">关闭</button>
            </div>
        </div>
    </div>
</div>
{include file='common/footer'}
<script>
    layui.use(['form','layedit'], function(){
        var form = layui.form;

        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: 'upload' //接口url
                ,type: 'post' //默认post
            }
        });
        layedit.build('content', {
            height: 180 //设置编辑器高度
        });
        var index = layedit.build('content'); //建立编辑器


        //获取角色
        form.on('select(bank_id)', function(data){
            var bank_id=$('select[name="bank_id"] option:checked').val();
            var html='';
            $.ajax({
                url: '{:url("admin/Role/ApiGetRolePage")}',
                type: 'POST',
                async:false,
                data:{bank_id:bank_id},
                dataType:'json',
                success: function (obj) {
                    var data =obj.data;
                    $.each(data,function(i,v){
                        html+='<option value="'+v.role_id+'">'+v.name+'</option>';
                    });
                    $('select[name="role_id"]').html("<option value=''>没有内容了</option>"+html);
                    form.render('select'); //这个很重要
                }
            });
        });


        //提交表单
        var url='{$url}';
        form.on('submit(demo1)', function(data){
            var  content = layedit.getContent(index);
            var  pre_content = layedit.getText(index);
            data.field['content'] = content;
            data.field['pre_content'] = pre_content;
            $.post(url,data.field,function (obj) {
                if(obj.code==200){
                    parent.layer.msg(obj.msg, {icon: 6,time: 1000});
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index,data.field); //再执行关闭
                }
                if(obj.code==400){
                    parent.layer.msg(obj.msg, {icon: 5,time: 1000});
                }
            },"JSON");
        });

        $('#closeIframe').click(function(){
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });

    });
</script>

</body>
</html>